<?php
/**
 * DiscussionExpirationForm class. This is where all the Admin side forms are generated
 */
class DiscussionExpirationForm extends PostBackControl
{
    var $ConfigurationManager;
    var $CategoryCheckboxes;
    var $AllowedCategory;
    var $List = "";
    var $Row = 0;
    var $Error = 0;
    var $NumRows = 0;
    var $PageNo = 0;
    var $LastPage = 0;

    function DiscussionExpirationForm(&$Context)
    {
        $this->Name = 'DiscussionExpirationForm';
        $this->ValidActions = array('DiscussionExpiration','ProcessDiscussionExpiration');
	$this->Constructor($Context);
        if($this->IsPostBack)
        {
            $query = 'SELECT count(*) FROM '.$this->Context->Configuration["DATABASE_TABLE_PREFIX"].'Discussion WHERE Closed = "0" AND DateExpire > 0000-00-00 AND Active = "1" AND WhisperUserID = 0;';
            $result = mysql_query($query);
            $query_data = mysql_fetch_row($result);
            $this->NumRows = $query_data[0];

            $SettingsFile = $this->Context->Configuration['APPLICATION_PATH'].'conf/settings.php';
            $this->ConfigurationManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'ConfigurationManager');
            if ($this->PostBackAction == 'ProcessDiscussionExpiration')
            {
                $this->ConfigurationManager->GetSettingsFromForm($SettingsFile);
                $this->ConfigurationManager->DefineSetting('DISCUSSIONEXPIRATION_CATEGORIES', implode(",", $_POST['AllowedCategory']), 0);

		$this->DelegateParameters['ConfigurationManager'] = &$this->ConfigurationManager;
                // And save everything
		if ($this->ConfigurationManager->SaveSettingsToFile($SettingsFile)) {
		    header('location: '.GetUrl($this->Context->Configuration, 'settings.php', '', '', '', '', 'PostBackAction=DiscussionExpiration&Success=1'));
		} else {
		    $this->PostBackAction = 'DiscussionExpiration';
		}
            }
        }
    }
    function GetExpirationList ()
    {
        $this->PageNo = ForceIncomingInt('PageNo',1);
        $rows_per_page = 10;
        $this->LastPage = ceil($this->NumRows/$rows_per_page);
        if ($this->PageNo < 1) $this->PageNo = 1;
        else if ($this->PageNo > $this->LastPage) $this->PageNo = $this->LastPage;
        $limit = 'LIMIT ' .($this->PageNo - 1) * $rows_per_page .',' .$rows_per_page;
        $this->Row = ($this->PageNo-1)*$rows_per_page;
        $query = 'SELECT DiscussionID, Name, DateExpire FROM '.$this->Context->Configuration["DATABASE_TABLE_PREFIX"].'Discussion WHERE Closed = "0" AND DateExpire > 0000-00-00 AND Active = "1" AND WhisperUserID = "0" ORDER BY DateExpire ASC '.$limit.';';
        $result = mysql_query($query);
        if (!$result) {
            $this->List = $this->Context->GetDefinition('ErrDiscussionExpiresList');
            $this->Error = 1;
        }
        else {
            while ($row = mysql_fetch_assoc($result)) {
                $this->Row++;
                $class = $this->Row%2==0 ? " class=\"alt\"" : "";
                list($yyyy, $mm, $dd) = explode('-',$row["DateExpire"]);
                $row["DateExpire"] = date('F j, Y', mktime(0,0,0,$mm,$dd,$yyyy));
                $url = GetUrl($this->Context->Configuration, 'comments.php', '', 'DiscussionID', $row["DiscussionID"], '', '#Item_1', CleanupString($row["Name"]).'/');
                $this->List .='<tr>
                     <th'.($this->Row%2==0 ? " class=\"rowalt\"" : " class=\"row\"").'>'.$this->Row.'</td>
                     <td'.$class.'><a href="'.$url.'">'.$row["Name"].'</a></td>
                     <td'.$class.'>'.$row["DateExpire"].'</td>
                     </tr>';
            }
            mysql_free_result($result);
        }
    }

    function Render()
    {
        if ($this->IsPostBack)
	{
            $this->PostBackParams->Clear();
	    if ($this->PostBackAction == 'DiscussionExpiration')
            {
                $this->AllowedCategory = explode(",", $this->Context->Configuration["DISCUSSIONEXPIRATION_CATEGORIES"]);
                // Load the category selector
		$cm = $this->Context->ObjectFactory->NewContextObject($this->Context, 'CategoryManager');
		$CategoryData = $cm->GetCategories(0, 1);
		$cat = $this->Context->ObjectFactory->NewObject($this->Context, 'Category');
		while ($Row = $this->Context->Database->GetRow($CategoryData)) {
			$cat->Clear();
			$cat->GetPropertiesFromDataSet($Row);
                        $this->CategoryCheckboxes .= "<li><p><span>".GetDynamicCheckBox("AllowedCategory[]", $cat->CategoryID,
                                                    (in_array($cat->CategoryID, $this->AllowedCategory)) ? 1 : 0,
                                                    "",$cat->Name,"","Category_".$cat->CategoryID )."</span></p></li>\r\n";
		}

                $this->PostBackParams->Set('PostBackAction', 'ProcessDiscussionExpiration');
                $ThemeFilePath = $this->Context->Configuration['EXTENSIONS_PATH'].'DiscussionExpiration/theme/Theme.DiscussionExpirationForm.php';
                include($ThemeFilePath);
            }
        }
    }
}
?>
